package com.ruoyi.yey.parent;


import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.yey.bill.BillEntity;
import com.ruoyi.yey.bill.BillService;
import com.ruoyi.yey.config.utils.PageUtil;
import com.ruoyi.yey.config.utils.PageVo;
import com.ruoyi.yey.parent.model.ParentAddModel;
import com.ruoyi.yey.parent.model.ParentEditModel;
import com.ruoyi.yey.parent.model.ParentSelectModel;
import com.ruoyi.yey.parent.student.ParentStudentService;
import com.ruoyi.yey.student.StudentEntity;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Api(tags = "家长管理")
@RequestMapping("/api/sys/parent")
@RestController
public class ParentController {

//    @Autowired
//    private ClassService classService;

    @Autowired
    private ParentService parentService;

    @Autowired
    private ParentStudentService parentStudentService;




    /**
     * 更新订单之中的积分到用户表里
     * @return
     */
    @GetMapping("/infoJiFen")
    public AjaxResult infoJiFen(){
        return  parentService.infoJiFen();
    }

    /**
     * 积分列表
     * @return
     */
    @GetMapping("/jiFenList")
    public AjaxResult jiFenList(){
        return parentService.jiFenList();
    }


    @ApiOperation("查询分页")
    @GetMapping("/findAllByPage")
    public Object findAllByPage(PageVo page, ParentEntity searchModel) {
        Page<Map> pageData = parentService.findAllByPage(PageUtil.initMpPage(page),searchModel);
        for (Map record : pageData.getRecords()) {
            //根据parent_id 获取student_info
            List<StudentEntity> studentEntityList = parentStudentService.getParentStudent(Integer.parseInt(record.get("id").toString()));
            if(CollUtil.isNotEmpty(studentEntityList)){
                record.put("studentNameList",studentEntityList.stream().map(item->item.getStudentName()).distinct().collect(Collectors.toList()));
            }
        }
        return pageData;
    }


    @ApiOperation("添加")
    @PostMapping("/add")
    public void saveUser(@RequestBody @Validated ParentAddModel parent) {
        parentService.add(parent);
    }


    @ApiOperation("根据id查询家长信息")
    @GetMapping("/findById/{id}")
    public ParentSelectModel findById(@PathVariable int id){
        ParentSelectModel parent = parentService.findStuById(id);
        if (ObjectUtil.isEmpty(parent)){
            parent = parentService.findById(id);
            return parent;
        }
        List<Object> studentIdList = new ArrayList<>();

        //返回一个id拼接的数组
        if(ObjectUtil.isNotEmpty(parent.getStudentList())){        //对应的学生列表不为空
            for (StudentEntity student:parent.getStudentList()){
                //获取学生id
                int studentId = student.getId();
                //获取班级id
                int classId = student.getClassId();
                //获取幼儿园的id
            }
            parent.setKgClassStuIdList(studentIdList);
        }
        return parent;
    }


    @ApiOperation("修改")
    @PostMapping("/edit")
    public void editKg(@RequestBody @Validated ParentEditModel parent) {
        parentService.edit(parent);
    }


    @ApiOperation("删除")
    @DeleteMapping(value = "/delByIds/{ids}")
    public void delByIds(@PathVariable int ids) {
        parentService.delByIds(ids);
    }
}
